<?php

class DBU_Db_Result {

	private $data;
	private $adapter;
	private $results = array();


	public function __construct($data, &$adapter) {
		$this->data 	= $data;
		$this->adapter 	= $adapter;
	}


	/**
	 * Frees result data on destruction
	 *
	 */
	public function __destruct() {
		if ($this->data && !is_bool($this->data)) {
			$this->adapter->freeResults($this->data);
		}
	}


	/**
	 * Fetches row from current result set
	 *
	 * @return array
	 */
	public function fetchResult() {
		try {
			return $this->adapter->fetchAssoc($this->data);
		} catch (Nip_Db_Exception $e) {
			$e->log();
		}
	}


   /**
	* Fetches all rows from current result set
	*
	* @return matrix
	*/
	public function fetchResults() {
		if (count($this->results) == 0) {
			while ($result = $this->fetchResult()) {
				$this->results[] = $result;
			}
		}
		return $this->results;
	}


	/**
	 * Fetches the total number of rows within the current result set
	 *
	 * @return numeric
	 */
	public function numRows() {
		return $this->adapter->numRows($this->data);
	}
}